package com.qls.ali.biz.data.controller;

import com.qls.ali.biz.service.fz.Cache;
import com.qls.ali.biz.service.fz.HotelService;
import com.qls.ali.biz.service.mybatis.HtHotelService;
import com.qls.core.dto.PushHotelBody;
import com.qls.core.entity.DtStore;
import com.qls.core.entity.HtHotel;
import com.qls.core.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;


/**
 * @author yunchao
 */
@RestController
@RequestMapping("hotel")
@Slf4j
public class HotelController {


    @Autowired
    private HotelService hotelService;

    @Autowired
    private Cache cache;

    @Autowired
    private HtHotelService htHotelService;

    @PostMapping("push")
    public R push(@RequestBody PushHotelBody pushHotelBody) {
        Integer storeId = pushHotelBody.getStoreId();
        Integer type = pushHotelBody.getType();
        Long hotelId = pushHotelBody.getHotelId();
        DtStore dtStore = cache.getStore(storeId);
        if (Objects.isNull(dtStore)) {
            String msg = String.format("酒店推送，推送类型店铺id：%s，type：%d，酒店id：%d，未设置店铺缓存", type, storeId, hotelId);
            log.warn(msg);
            return R.error(msg);
        }
        HtHotel htHotelinfo = htHotelService.getHotelAll(hotelId);
        if (Objects.isNull(htHotelinfo)) {
            String msg = String.format("酒店推送，店铺id：%s，推送类型type：%d，酒店id：%d，酒店关联查询结果为 null", type, storeId, hotelId);
            log.warn(msg);
            return R.error(msg);
        }
        String appKey = dtStore.getAppKey();
        String session = dtStore.getSessionKey();
        String secret = dtStore.getSecret();
        switch (type) {
            case -1:
                hotelService.delAll(hotelId, storeId, appKey, secret, session);
                break;
            case -2:
                hotelService.del(hotelId, storeId, appKey, secret, session);
                break;
            case 1:
                hotelService.pushAll(htHotelinfo, storeId, appKey, secret, session);
                break;
            case 2:
                hotelService.push(htHotelinfo, storeId, appKey, secret, session);
                break;
            default:
                String msg = String.format("酒店推送，店铺id：%s，推送类型type：%d，酒店id：%d，推送类型错误", type, storeId, hotelId);
                log.warn(msg);
                return R.error(msg);
        }
        return R.ok();
    }

}
